package com.tsing.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.pojo.vo.ArticleDetailsVO;
import com.tsing.pojo.vo.DetailsVo;
import com.tsing.pojo.vo.DynamicVO;
import com.tsing.service.DynamicService;
import com.tsing.service.impl.DynamicServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @auth 卢孝宇
 * @date 2023/12/13 16:49
 */
public class DynamicController {

    private DynamicService dynamicService = new DynamicServiceImpl();
    //查询动态需要的数据
    public PageResult<DynamicVO> dynamicList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = (Integer) jsonObject.get("userId");
        userId = (userId != null && userId > 0) ? userId : 1;
        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;
        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;


        PageResult<DynamicVO> result = dynamicService.dynamic(userId, page, size);

        return result;
    }

    //获取帖子查看全文的详细信息
    public Result<DetailsVo> detailsList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer postId = (Integer) jsonObject.get("postId");
        postId = (postId != null && postId > 0) ? postId : null;


        Integer userId = (Integer) jsonObject.get("userId");
        userId = (userId != null && userId > 0) ? userId : null;


        Result<DetailsVo> result = dynamicService.detailsList(postId,userId);

        return result;
    }
}
